var app = getApp()
var base64 = require('../../utils/base64.js');
Page({
  data: {
    redpacket_id: 0,//选中红包id
    glist: '',//红包列表
    count_price: '',
    redpacket_price: '',
    redpacket_limit: '',
    rpt_info: [],
    typ: '',
    redPacketList:''
  },
  onLoad: function (options) {
    var that = this
    if (options.typ == 'ziti') {
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];   //当前页面
      var prevPage = pages[pages.length - 2];
      var redPacketList = prevPage.data.order.redPacketList
      var redpacket_id = prevPage.data.redPacketId
      
      that.setData({
        redPacketList: redPacketList,
        redpacket_id: redpacket_id,
        typ: 'ziti'
      })
    } else {
      var glist = JSON.parse(base64.decode(decodeURIComponent(options.glist)))
      var redpacket_price = options.redpacket_price
      var redpacket_limit = options.redpacket_limit
      var redpacket_id = options.redpacket_id
      var count_price = options.count_price
      if (glist.rpt_list) {
        if (glist.rpt_list && redpacket_id) {
          for (var j in glist.rpt_list) {
            if (glist.rpt_list[j] == redpacket_id) {
              redpacket_id = glist.rpt_list[j].id
            }
          }
        }

        that.setData({
          glist: glist.rpt_list,
          count_price: count_price,
          redpacket_price: redpacket_price,
          redpacket_limit: redpacket_limit,
          redpacket_id: redpacket_id,
          rpt_info: glist.rpt_info
        })
      }
    }

  },
  //门店自提订单红包选中
  receives: function (e) {
    var that = this
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];
    var redPacketDiscountMsg = '';
    var redpacket_id = e.currentTarget.dataset.redpacket_id;
    var redpacket_limit = e.currentTarget.dataset.redpacket_limit;
    var redpacket_price = e.currentTarget.dataset.redpacket_price;
    if (redpacket_id == 0) {
      if (prevPage.data.voucherId > 0) {
        var discountedOrderAmount = Number(prevPage.data.orderAmount) - Number(prevPage.data.voucherDiscount)
      } else {
        var discountedOrderAmount = Number(prevPage.data.orderAmount)
      }
      var isUserOptOutRedPacket = 1
      redPacketDiscountMsg = '不使用平台红包'

    } else {
      if (prevPage.data.redPacketId > 0) {
        var discountedOrderAmount = Number(prevPage.data.orderAmount) - Number(redpacket_price) - Number(prevPage.data.voucherDiscount)
      } else {
        var discountedOrderAmount = Number(prevPage.data.orderAmount) - Number(redpacket_price)
      }
      var isUserOptOutRedPacket = 0
      redPacketDiscountMsg = "满" + redpacket_limit + "减" + redpacket_price;
    }

    that.setData({
      redpacket_id: redpacket_id
    })
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      discountedOrderAmount: discountedOrderAmount,
      isUserOptOutRedPacket: isUserOptOutRedPacket,
      redPacketDiscountMsg: redPacketDiscountMsg,
      redPacketDiscount: redpacket_price,
      redPacketId: redpacket_id
    })
    wx.navigateBack({
      delta: 1
    })

  },
  //红包选中
  receive: function (e) {
    var that = this
    var redpacket_price = e.currentTarget.dataset.redpacket_price
    var redpacket_id = e.currentTarget.dataset.redpacket_id
    var redpacket_limit = e.currentTarget.dataset.redpacket_limit


    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];
    var glist = prevPage.data.glist

    if (redpacket_price > 0 && redpacket_id > 0) {
      glist.rpt_info[0].id = redpacket_id
      glist.rpt_info[0].limit = redpacket_limit
      glist.rpt_info[0].price = redpacket_price
      glist.best_rpt_info = '满' + redpacket_limit + '减' + redpacket_price
      var order_price = Number(that.data.count_price) + Number(that.data.redpacket_price) - Number(redpacket_price)
    } else {
      var order_price = Number(that.data.count_price) + Number(that.data.redpacket_price)
      glist.rpt_info[0].id = 0
      glist.rpt_info[0].limit = 0
      glist.rpt_info[0].price = 0
      glist.best_rpt_info = '不使用平台红包'
    }

    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      count_price: order_price,
      redpacket_id: redpacket_id,
      glist: glist
    })
    wx.navigateBack({
      delta: 1
    })

  }
})